package com.wang.auth.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wang.auth.mapper.MenuMapper;
import com.wang.auth.service.MenuService;
import com.wang.model.wechat.Menu;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.model.wechat.vo.MenuVo;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private WxMpService wxMpService;
    @Override
    public List<MenuVo> findMenuInfo() {
        //1.查询所有菜单list集合
        List<Menu> menus = baseMapper.selectList(null);
        //2.查询所有一级菜单parent_id=0，返回一级菜单List集合
        ArrayList<Menu> oneMenuList = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0){
                oneMenuList.add(menu);
            }
        }
        //3.一级菜单list集合遍历，得到每一个一级菜单
        ArrayList<MenuVo> menuVos = new ArrayList<>();
        for (Menu noeMenu : oneMenuList) {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(noeMenu,menuVo);
            //4.获取每一个一级菜单里的所有的二级菜单id和parent_id比较
            List<Menu> collect = menus.stream()
                    .filter(menu -> Objects.equals(menu.getParentId(), noeMenu.getId()))
                    .collect(Collectors.toList());

            //把每一个一级菜单的所有二级菜单封装到一级菜单中的children集合里面
            List<MenuVo> children = new ArrayList<>();
            for (Menu menu : collect) {
                MenuVo menuVo1 = new MenuVo();
                BeanUtils.copyProperties(menu,menuVo1);
                children.add(menuVo1);
            }
            menuVo.setChildren(children);
            menuVos.add(menuVo);
            //涉及到Menu和MenuVo的转换
        }

        return menuVos;
    }

    @Override
    public void createMenu() {
        //把菜单的数据查询出来，封装成微信菜单格式
        //调用工具里面的方法实现菜单推送
        List<MenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://oawang.vipgz1.91tunnel.com/#"+oneMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if(twoMenuVo.getType().equals("view")) {
                        view.put("name", twoMenuVo.getName());
                        //H5页面地址
                        view.put("url", "http://oawang.vipgz1.91tunnel.com/#"+twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMenuKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}
